package cn.edu.tju.postfix;

public class InToPost {
	

	private String input;
	private String output = "";
	private Stack stack;
	
	public InToPost(String input){
		this.input = input;
		stack = new Stack(input.length());
	}
	
	public String doTrans(){
		for(int i = 0; i < input.length(); i ++){
			char ch = input.charAt(i);
			switch(ch){
			case '*':
			case '/':
				while(!stack.isEmpty() && (stack.getTop() == '*' || stack.getTop() == '/')){
					output += stack.pop();
				}
				stack.push(ch);
				break;
			case '+':
			case '-':
				while(!stack.isEmpty() && stack.getTop() != '('){
					output += stack.pop();
				}
				stack.push(ch);
				break;
			case '(':
				stack.push(ch);
				break;
			case ')':
				char temp;
				while(!stack.isEmpty() && ((temp = stack.pop()) != '(')){
					output += temp;	
				}
				break;
			default:
				output += ch;
				break;
			}
		}
		while(!stack.isEmpty()){
			output += stack.pop();
		}
		return output;
	}
	
	

}
